/*
 * Copyright (C) 2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package top.xbaistack.config;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * Spring 静态上下文维护类，可以通过其中的方法在静态方法中获取 Spring 上下文中的 Bean。
 *
 * @author xbaistack
 * @since 2025/03/25
 */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public final class SpringContextHolder implements ApplicationContextAware, DisposableBean, Ordered {

    private static ApplicationContext context;

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        SpringContextHolder.context = context;
    }

    @Override
    public void destroy() throws Exception {
        SpringContextHolder.context = null;
    }

    public static boolean isReady() {
        return context != null;
    }

    public static ApplicationContext context() {
        synchronized (context) {
            return context;
        }
    }

    public static Environment getEnvironment() {
        return context().getEnvironment();
    }

    public static String getPrimaryProfile() {
        Environment environment = context().getEnvironment();
        String[] activeProfiles = environment.getActiveProfiles();
        return ArrayUtils.isNotEmpty(activeProfiles) ? activeProfiles[ 0 ] : "dev";
    }

    public static boolean isNonProduction() {
        return StringUtils.equalsIgnoreCase(getPrimaryProfile(), "prod");
    }

    public static boolean containsBean(String name) {
        return context().containsBean(name);
    }

    public static <T> T getBean(Class<T> requiredType) {
        return context().getBean(requiredType);
    }

    public static <T> T getBean(String beanName, Class<T> type) {
        return context().getBean(beanName, type);
    }

    public static Object getBean(String beanName) {
        return context().getBean(beanName);
    }

    public static Object getBean(String beanName, Object... args) {
        return context().getBean(beanName, args);
    }

    public static <T> List<T> getBeans(Class<T> requiredType) {
        ApplicationContext context = context();
        String[] beanNames = context.getBeanNamesForType(requiredType);
        if (ArrayUtils.isEmpty(beanNames)) {
            return Collections.emptyList();
        }
        List<T> beans = new ArrayList<T>(beanNames.length);
        for (String beanName : beanNames) {
            beans.add(context.getBean(beanName, requiredType));
        }
        return beans;
    }

}
